package com.qtt.app.service.impl;

import com.qtt.app.entity.User;
//import com.qtt.app.mapper.AppUserTestMapper;
import com.qtt.app.mapper.UserMapper;
import com.qtt.app.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.qtt.app.utils.RandomUtils.getRandom;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
//    AppUserTestMapper appUserTestMapper;

    @Override
    public List<User> getAll() {
        List<User> entities = userMapper.getAll();
        return entities;
    }

    @Override
    public User getById(Long userId) {
        User entities = userMapper.getById(userId);
        return entities;
    }


    @Override
    public int appUserRegister(String phone, String nickname, String realName, String password, String company, String position) {
        Date gmtCreate = new Date();
        System.out.println(gmtCreate);
        int flag=userMapper.register(phone, nickname, realName, password, company, position,gmtCreate);
        //返回插入数据后的记录id
        int id;
        if(flag == 1){
            id = userMapper.getLastId();
        }
        else{
            id = 0;
        }
        return id;
    }

    @Override
    public Long getUserByPhone(String phone) {
        Long id = userMapper.getUserByPhone(phone);
        Long wrongId = new Long((long)0);
        if(id==null){return wrongId;}
        return id;
    }

    @Override
    public Boolean deleteById(Long userId) {
        Boolean b = userMapper.deleteById(userId);
        return b;
    }

    @Override
    public Boolean updateById(Long id, User entity) {
        Boolean b = userMapper.updateById(id,entity);
        return b;
    }

    @Override
    public int addUser(User entity) {
        int b = userMapper.addUser(entity);
        return b;
    }

    @Override
    public Boolean cancelUser(Long id) {
        User entity = new User();
        entity.setUserId(id);
        entity.setOpenid("cancelOpenid"+getRandom(15));
        entity.setPhone("cancelPhone"+getRandom(15));
        entity.setCompany(" ");
        entity.setPosition(" ");
        entity.setNickname("注销用户"+getRandom(7));
        entity.setSex(0);
        entity.setHead("https://qtt-cmall.oss-accelerate.aliyuncs.com/rotation/202105/1621435346255icon-注册头像.png");
        entity.setGmtModified(new Date());
        Boolean b = userMapper.cancelUser(id,entity);
        return b;
    }


}
